/**
 * Created by Admin on 2017/6/7.
 */
function xxk() {
    var xxk = document.getElementById("xxk");
    var tcbc = document.getElementsByClassName("tcbc");

    for (var i = 0; i < xxk.children.length; i++) {
        xxk.children[i].index = i;
        xxk.children[i].onclick = function () {
            for (var i = 0; i < xxk.children.length; i++) {
                xxk.children[i].className = '';
                tcbc[0].children[i].style.display = 'none';
            }
            this.className = 'on';
            tcbc[0].children[this.index].style.display = 'block';
        }
    }
};


function check() {
    var params = {};
    params.artTitle = $("#art_title").val();
    params.artContext = $("#art_context").val();
    params.userAccount = name;
    params.artId = "";
    $.ajax({
        type: "POST",
        url: "/art/add",
        dataType: "text",
        async: false,
        data: params,
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert("出现异常，异常原因【" + data + "】!");
        }
    });

}

function deleteArt(obj) {

    var deleteId = $(obj).attr("class");

    $.ajax({
        type: "get",
        url: "/art/deleteArt",
        dataType: "text",
        async: false,
        data: {"artId": deleteId},
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert("出现异常，异常原因【" + data + "】!");
        }
    });
}

function addFab(obj) {

    var fabId = $(obj).attr("class");

    $.ajax({
        type: "get",
        url: "/fab/addFab",
        dataType: "text",
        async: false,
        data: {"fabId": fabId, "userAccount": name},
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert("出现异常，异常原因【" + data + "】!");
        }
    });
}

function ajax() {

    var list;

    $.ajax({
        async: false,
        type: 'post',
        url: '/art/username',
        data: {"userName": name},
        dataType: 'json',
        success: function (data) {
            list = eval(data);
            for (var i = 0; i < list.length; i++) {
                addArtOnPage(list[i].userAccount, list[i].artTitle, list[i].artContext,list[i].artId)
            }
        },
        error: function (data) {
            alert("JSON数据获取失败，请联系管理员！");
        }
    });
}
function addArtOnPage(a, b, c,d) {
    var str = "<div class='art'><h3><a href='##'>" + a + "</a>发布博客:<a href='#'>" + b + "</a></h3> <p>" + c + "</p> <div class='art_column'> <ul> <li class='"+d+"' onclick='addFab(this)' >赞</li>  <li class='"+d+"' onclick='deleteArt(this)'>删除</li></ul> </div> </div>";
    var lyh = $('#lyh');
    lyh.append(str);
}



function ajaxTwo() {

    var list;

    $.ajax({
        async: false,
        type: 'get',
        url: '/art/load',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            list = eval(data);
            for (var i = 0; i < list.length; i++) {
                if (list[i].userAccount == name) {
                    addArtOnPageAll(list[i].userAccount, list[i].artTitle, list[i].artContext, list[i].artId);
                } else {
                    addArtOnPageTwo(list[i].userAccount, list[i].artTitle, list[i].artContext, list[i].artId);
                }
            }

        },
        error: function (data) {
            alert("JSON数据获取失败，请联系管理员！");
        }
    });
}

function addForword(obj) {
    var Id = $(obj).attr("id")

    $.ajax({
        type: "get",
        url: "/forword/insert",
        dataType: "text",
        async: false,
        data: {"forwordId": Id, "forwordUser": name},
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert("出现异常，异常原因【" + data + "】!");
        }
    });
}

function addFabAll(obj) {

    var fabId = $(obj).attr("class");

    $.ajax({
        type: "get",
        url: "/fab/addFab",
        dataType: "text",
        async: false,
        data: {"fabId": fabId, "userAccount": name},
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert("出现异常，异常原因【" + data + "】!");
        }
    });

}

function addArtOnPageTwo(a, b, c, d) {
    var str = "<div class='art'><h3><a href='##'>" + a + "</a>发布博客:<a href='#'>" + b + "</a></h3> <p>" + c + "</p> <div class='art_column'> <ul> <li class='" + d + "' onclick='addFabAll(this)' >赞</li> <li onclick='addForword(this)' id='" + d + "'>转发</li> </ul> </div> </div>";
    var lyh = $('#lyhAll');
    lyh.append(str);
}
function addArtOnPageAll(a, b, c, d) {
    var str = "<div class='art'><h3><a href='##'>" + a + "</a>发布博客:<a href='#'>" + b + "</a></h3> <p>" + c + "</p> <div class='art_column'> <ul> <li class='" + d + "' onclick='addFabAll(this)' >赞</li>  <li class='"+d+"' onclick='deleteArt(this)'>删除</li></ul> </div> </div>";
    var lyh = $('#lyhAll');
    lyh.append(str);
}

function addImg(){
    $.ajax({
        type: "post",
        url: "/user/addImg",
        dataType: "text",
        async: false,
        data: {"userAccount": name},
        success: function (data) {
            console.log(data);
            $("#imgId").attr("src","img/"+data);
        },
        error: function (data) {
            alert("出现异常，异常原因【" + data + "】!");
        }
    });
}

window.onload = function () {
    xxk();
    ajax();
    ajaxTwo();
    addImg();
}